06. Assembly Language
Assembly Language
In between C++ and machine code there is actually another language called assembly language. Assembly language is a human readable low-level language that gets you even closer to the hardware than C++.
Your compiler might not actually produce assembly language code and instead go directly to machine code. But you can still see assembly language code if you're curious. And there might be rare cases when you are trying to improve code efficiency and write assembly language directly in order to improve performance.
In the previous demonstration, you can use the following commands to output the assembly language code:
cd ~/home/workspace/intro_to_hardware
g++ -S machine_code.cpp
This will output a file called machine_code.s, which you can then double-click to see its contents.
Assembly language is not nearly as intuitive as C++, but it is still human readable. When you do something as simple as declaring and defining a variable,int x = 5;
. the computer has to break this up into a series of steps like assigning a space in memory to the variable x and then placing the value 5 into the assigned space.
You can write out each of these steps directly in assembly language. For the purposes of this course, you do not need to be familiar with assembly language. But just looking at assembly code will prove to you that every line of C++ code has consequences in terms of efficiency. Look at the number of steps involved with just assigning the value to x.
Unnecessary lines of code mean that the CPU will take more time to execute a program than what is actually needed.